﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml;
using System.IO;
using System.Windows.Forms;
//using DevExpress.XtraBars.Forms;
//using DevExpress.XtraEditors;
//using DevExpress.XtraGrid.Columns;
//using DevExpress.XtraReports;
//using DevExpress.XtraReports.UI;
using System.ComponentModel;

namespace AMS.Inventory.PDA.Castle.DataAccess
{
    public static class clsLocalization
    {

       private static string xmlPath = @"program files\AMS.Inventory.PDA\Resources\AppResource_ZH.xml";

        /// <summary> 
        /// 读取多语言的资源文件 
        /// </summary> 
        /// <param name="frmName">窗体的Name</param> 
        /// <param name="lang">要显示的语言(如ZH或EN)</param> 
        /// <returns></returns> 
        public static Hashtable ReadResource(string frmName, string lang)
        {
            Hashtable result = new Hashtable();
            XmlReader reader = null;
            FileInfo fi = null;
            fi = new FileInfo(xmlPath);
            if (!fi.Exists)
                reader = null;
               // reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "resources/AppResource.xml");
            else
                reader = new XmlTextReader(xmlPath);

            //if (!fi.Exists)
            //    reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "resources/AppResource.xml");
            //else
            //    reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "resources/AppResource_" + lang + ".xml");

            XmlDocument doc = new XmlDocument();
            doc.Load(reader);

            XmlNode root = doc.DocumentElement;
            XmlNodeList nodelist = root.SelectNodes("Form[Name='" + frmName + "']/Controls/Control");
            foreach (XmlNode node in nodelist)
            {
                try
                {
                    XmlNode node1 = node.SelectSingleNode("@name");
                    XmlNode node2 = node.SelectSingleNode("@text");
                    if (node1 != null)
                    {
                        result.Add(node1.InnerText.ToLower(), node2.InnerText);
                    }
                }
                catch (FormatException fe)
                {
                    Console.WriteLine(fe.ToString());
                }
            }
            reader.Close();

            return result;
        }

        /// <summary> 
        /// 读取多语言的资源文件 
        /// </summary> 
        /// <param name="frmName">窗体的Name</param> 
        /// <param name="lang">要显示的语言(如ZH或EN)</param> 
        /// <returns></returns> 
        public static Hashtable ReadContectResource(string frmName, string lang)
        {
            Hashtable result = new Hashtable();

            XmlReader reader = null;
            FileInfo fi = null;
            fi = new FileInfo(xmlPath);
            if (!fi.Exists)
                reader = null;
            // reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "resources/AppResource.xml");
            else
                reader = new XmlTextReader(xmlPath);
            //fi = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "resources/AppResource_" + lang + ".xml");
            //if (!fi.Exists)
            //    reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "resources/AppResource.xml");
            //else
            //    reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "resources/AppResource_" + lang + ".xml");

            XmlDocument doc = new XmlDocument();
            doc.Load(reader);

            XmlNode root = doc.DocumentElement;
            XmlNodeList nodelist = root.SelectNodes("Form[Name='" + frmName + "']/Contents/Content");
            foreach (XmlNode node in nodelist)
            {
                try
                {
                    XmlNode node1 = node.SelectSingleNode("@name");
                    XmlNode node2 = node.SelectSingleNode("@text");
                    if (node1 != null)
                    {
                        if(!result.Contains(node1))
                        result.Add(node1.InnerText.ToLower(), node2.InnerText);
                    }
                }
                catch (FormatException fe)
                {
                    Console.WriteLine(fe.ToString());
                }
            }
            reader.Close();

            return result;
        }

        /// <summary> 
        /// 获取控件的名称 
        /// </summary> 
        /// <param name="form"></param> 
        //public static void getControlNames(BaseControl.ControlBase form)
        //{ 
        //    //根据用户选择的语言获得表的显示文字 
        //    Hashtable table = ReadResource(form.Name,modCommon.DefaultLanguage);    // Global.GetValue("lang").ToString()); 
        //    Control.ControlCollection controlNames = form.Controls; 
       
        //    try
        //    {
        //        foreach (Control control in controlNames)
        //        {
        //            if (control.GetType() == typeof(System.Windows.Forms.Panel))
        //                GetSubControls(control.Controls, table);

        //            if (control.GetType() == typeof(System.Windows.Forms.GroupBox))
        //                GetSubControls(control.Controls, table);

        //            if(control.GetType() == typeof(DevExpress.XtraEditors.PanelControl))
        //                GetSubControls(control.Controls, table);

        //            if (control.GetType() == typeof(DevExpress.XtraEditors.GroupControl))
        //                GetSubControls(control.Controls, table);

        //            if (control.GetType() == typeof(DevExpress.XtraGrid.GridControl))
        //            {
        //                DevExpress.XtraGrid.GridControl gdControl =(DevExpress.XtraGrid.GridControl) control;
        //                GetSubGridViewControls(gdControl.ViewCollection, table);
        //            }

        //            if (control.GetType() == typeof(DevExpress.XtraReports.UI.XRTable))
        //                GetSubControls(control.Controls, table);


        //            if (table.Contains(control.Name.ToLower()))
        //                control.Text = (string)table[control.Name.ToLower()];
        //        }
        //        if (table.Contains(form.Name.ToLower()))
        //            form.Text = (string)table[form.Name.ToLower()];
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.ToString());
        //    }
        //}

        /// <summary> 
        /// 获取控件的名称 
        /// </summary> 
        /// <param name="form"></param> 
        public static void getControlNames(Form form)
        {
            //根据用户选择的语言获得表的显示文字 
            Hashtable table = ReadResource(form.Name, "");    // Global.GetValue("lang").ToString()); 
            Control.ControlCollection controlNames = form.Controls;

            try
            {
                foreach (Control control in controlNames)
                {
                    if (control.GetType() == typeof(System.Windows.Forms.Panel))
                        GetSubControls(control.Controls, table);

                    //if (control.GetType() == typeof(System.Windows.Forms.GroupBox))
                    //    GetSubControls(control.Controls, table);

                    //if (control.GetType() == typeof(DevExpress.XtraEditors.PanelControl))
                    //    GetSubControls(control.Controls, table);

                    //if (control.GetType() == typeof(DevExpress.XtraEditors.GroupControl))
                    //    GetSubControls(control.Controls, table);

                    //if (control.GetType() == typeof(DevExpress.XtraReports.UI.XRTable))
                    //    GetSubControls(control.Controls, table);

                    //if (control.GetType() == typeof(DevExpress.XtraGrid.GridControl))
                    //{
                    //    DevExpress.XtraGrid.GridControl gdControl = (DevExpress.XtraGrid.GridControl)control;
                    //    GetSubGridViewControls(gdControl.ViewCollection, table);
                    //}

                    //if (control.GetType() == typeof(DevExpress.XtraNavBar.NavBarControl))
                    //{
                    //    DevExpress.XtraNavBar.NavBarControl navbarControl = (DevExpress.XtraNavBar.NavBarControl) control;
                    //    GetSubNavBarGroups(navbarControl.Groups, table);
                    //    GetSubNavBarItems(navbarControl.Items, table);
                    //}                   

                    if (table.Contains(control.Name.ToLower()))
                        control.Text = (string)table[control.Name.ToLower()];
                }

                if (table.Contains(form.Name.ToLower()))
                    form.Text = (string)table[form.Name.ToLower()];
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        //private static void GetSubMenuBarItems(DevExpress.XtraBars.BarItems barItems, Hashtable table)
        //{
        //    foreach (DevExpress.XtraBars.BarItem control in barItems)
        //    {
        //        if (table.Contains(control.Name.ToLower()))
        //            control.Caption = (string)table[control.Name.ToLower()];
        //    }
        //}

        //private static void GetSubNavBarGroups(DevExpress.XtraNavBar.NavGroupCollection navGroupCollection, Hashtable table)
        //{
        //    foreach (DevExpress.XtraNavBar.NavBarGroup control in navGroupCollection)
        //    {
        //        if (table.Contains(control.Name.ToLower()))
        //            control.Caption = (string)table[control.Name.ToLower()];
        //    }
        //}

        //private static void GetSubNavBarItems(DevExpress.XtraNavBar.NavItemCollection navItemCollection, Hashtable table)
        //{
        //    foreach (DevExpress.XtraNavBar.NavBarItem control in navItemCollection)
        //    {
        //        if (table.Contains(control.Name.ToLower()))
        //            control.Caption = (string)table[control.Name.ToLower()];
        //    }
        //}

        /// <summary> 
        /// 获取控件的名称 
        /// </summary> 
        /// <param name="form"></param> 
        //public static void getControlNames(XtraReport form)
        //{
        //    //根据用户选择的语言获得表的显示文字 
        //    Hashtable table = ReadResource(form.Name, modCommon.DefaultLanguage);    // Global.GetValue("lang").ToString()); 
        //    XRControlCollection controlNames = form.Controls;

        //    try
        //    {
        //        foreach (XRControl control in controlNames)
        //        {
        //            if (control.GetType() == typeof(PageHeaderBand))
        //                GetSubControls(control.Controls, table);

        //            if (control.GetType() == typeof(PageFooterBand))
        //                GetSubControls(control.Controls, table);

        //            if (control.GetType() == typeof(ReportHeaderBand))
        //                GetSubControls(control.Controls, table);

        //            if (control.GetType() == typeof(ReportFooterBand))
        //                GetSubControls(control.Controls, table);

        //            if (control.GetType() == typeof(TopMarginBand))
        //                GetSubControls(control.Controls, table);

        //            if (control.GetType() == typeof(BottomMarginBand))
        //                GetSubControls(control.Controls, table);

        //            if (control.GetType() == typeof(GroupHeaderBand))
        //                GetSubControls(control.Controls, table);

        //            if (control.GetType() == typeof(GroupFooterBand))
        //                GetSubControls(control.Controls, table);

        //            if (control.GetType() == typeof(DetailBand))
        //                GetSubControls(control.Controls, table);

        //            if (table.Contains(control.Name.ToLower()))
        //                control.Text = (string)table[control.Name.ToLower()];
        //        }
        //        if (table.Contains(form.Name.ToLower()))
        //            form.Text = (string)table[form.Name.ToLower()];
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.ToString());
        //    }
        //}


        /// <summary> 
        /// 获得子控件的显示名 
        /// </summary> 
        /// <param name="controls"></param> 
        /// <param name="table"></param> 
        private static void GetSubControls(Control.ControlCollection controls, Hashtable table)
        {
            foreach (Control control in controls)
            {
                if (control.GetType() == typeof(System.Windows.Forms.Panel))
                    GetSubControls(control.Controls, table);

                //if (control.GetType() == typeof(System.Windows.Forms.GroupBox))
                //    GetSubControls(control.Controls, table);

                //if (control.GetType() == typeof(DevExpress.XtraGrid.GridControl))
                //{
                //    DevExpress.XtraGrid.GridControl gdControl = (DevExpress.XtraGrid.GridControl)control;
                //    GetSubGridViewControls(gdControl.ViewCollection, table);
                //}

                //if (control.GetType() == typeof(DevExpress.XtraGrid.Views.Grid.GridView))
                //    GetSubControls(control.Controls, table);


                //if (control.GetType() == typeof(DevExpress.XtraReports.UI.XRTableRow))
                //    GetSubControls(control.Controls, table);

                if (control.GetType() == typeof(System.Windows.Forms.DataGrid))
                    GetSubControls(control.Controls, table);


                if (table.Contains(control.Name.ToLower()))
                    control.Text = (string)table[control.Name.ToLower()];
            }
        }

        //private static void GetSubControls(XRControlCollection controls, Hashtable table)
        //{
        //    foreach (XRControl control in controls)
        //    {
        //        if (control.GetType() == typeof(XRTable))
        //            GetSubControls(control.Controls, table);

        //        if (control.GetType() == typeof(XRTableRow))
        //            GetSubControls(control.Controls, table); 

        //        if (table.Contains(control.Name.ToLower()))
        //            control.Text = (string)table[control.Name.ToLower()];
        //    }
        //}

        //private static void GetSubGridViewControls(DevExpress.XtraGrid.Repository.ViewRepositoryCollection controls, Hashtable table)
        //{
        //    foreach (DevExpress.XtraGrid.Views.Grid.GridView control in controls)
        //    {
        //        GetSubGridColumnsControls(control, table);
        //    }
        //}

        //private static void GetSubGridColumnsControls(DevExpress.XtraGrid.Views.Grid.GridView controls, Hashtable table)
        //{
        //    foreach (DevExpress.XtraGrid.Columns.GridColumn control in controls.Columns)
        //    {
        //        if (table.Contains(control.Name.ToLower()))
        //            control.Caption = (string)table[control.Name.ToLower()];
        //    }
        //}

        /// <summary> 
        /// 获取窗体里的内容 
        /// </summary> 
        /// <param name="form"></param> 
        public static string  getFromContentNames(Hashtable table, string contentName)
        {
            string strName = "";
            try
            {
                if (table.Contains(contentName.ToLower()))
                    strName = (string)table[contentName.ToLower()];                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return strName;
        }

    }
}
